package com.naicheng.ivote.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.MyShareRes;
import com.naicheng.ivote.doman.MyquestionnaireRes;
import com.naicheng.ivote.engin.BaseActivity;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.volley.Response.Listener;

/**
 * 我的问卷
 * 
 * @author guoxin
 * 
 */
public class MyShareActivity extends BaseActivity {
	ListView lv_mineask;
	TextView main_title1;
	List<MyShareRes> list = new ArrayList<MyShareRes>();
	mAdapter adapter;
	TextView tv_back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mineask);
		lv_mineask = (ListView) this.findViewById(R.id.lv_mineask);
		main_title1 = (TextView) this.findViewById(R.id.main_title1);
		tv_back = (TextView) this.findViewById(R.id.tv_back);
		main_title1.setText("我的分享");
		
		tv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		getData();
	}

	/**
	 * 初始化请求
	 */
	private void getData() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userid", ConstantMine.userid);
		super.getDataFromServer("myshare", map,
				new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						System.out.println("response------>"
								+ response.toString());
						BaseResponseEntity<MyShareRes> mResponse = new BaseParser<MyShareRes>()
								.parserCollection(response, MyShareRes.class);
						if (mResponse != null && mResponse.getState()) {
							list = mResponse.getData();
							System.out.println("list.size=========="
									+ list.size());
							adapter = new mAdapter();
							lv_mineask.setAdapter(adapter);
						}

					}
				}, this);
	}

	class mAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			viewHolder holder = null;
			if (arg1 == null) {
				arg1 = View.inflate(getApplicationContext(),
						R.layout.item_myask, null);
				holder = new viewHolder();
				holder.tv_myask_title = (TextView) arg1
						.findViewById(R.id.tv_myask_title);
				holder.tv_myask_content = (TextView) arg1
						.findViewById(R.id.tv_myask_content);
				holder.tv_myask_time = (TextView) arg1
						.findViewById(R.id.tv_myask_time);
				holder.tv_myask_delete = (TextView) arg1
						.findViewById(R.id.tv_myask_delete);
				arg1.setTag(holder);
			} else {
				holder = (viewHolder) arg1.getTag();
			}
			MyShareRes data = list.get(arg0);
			if (data != null) {
				holder.tv_myask_title.setText(data.title);
				holder.tv_myask_content.setText(data.content);
				holder.tv_myask_time.setText(data.sharetime);
				holder.tv_myask_delete
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								delete(arg0);
							}
						});
				holder.tv_myask_delete.setText("清除");
			}
			return arg1;
		}

	}

	static class viewHolder {
		TextView tv_myask_title;
		TextView tv_myask_content;
		TextView tv_myask_time;
		TextView tv_myask_delete;
	}

	/**
	 * removeshare
	 * 
	 * @param position
	 */
	private void delete(final int position) {
		MyShareRes res = list.get(position);
		if (res != null) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("userid", ConstantMine.userid);
			map.put("shareid ", String.valueOf(res.id));

			super.getDataFromServer("removeshare", map,
					new Listener<JSONObject>() {

						@Override
						public void onResponse(JSONObject response) {
							System.out.println("response------>"
									+ response.toString());
							BaseResponseEntity<String> mResponse = new BaseParser<String>()
									.parserString(response);
							if (mResponse != null && mResponse.getState()) {// 删除成功，更新adapter

								list.remove(position);
								adapter.notifyDataSetChanged();
							}
						}
					}, this);
		}
	}

}
